<?php

autoLoader();

if( false == isset( $_POST['operation'] ) ) { 
	$str = '';
	$str .= '<form name=myform method=post action=modules.php?file=patient_admission&mid=' . $_REQUEST['mid'] . '&smid=' . $_REQUEST['smid'] . '>';
	$str .= '<script language=JavaScript>document.myform.submit();</script></form>';
	echo $str;
	exit;
}

if( true == isset( $_POST['operation'] ) && 'insert' == strtolower( $_POST['operation'] ) ) {
	
	$objWard 		= new Wards();
	$objPatient 	= new Patients();
	$objCasePaper 	= new CasePapers();
	
	$intChargeAmount = NULL;
	$intDiagnosisId = 'NULL';
	
	if( true == isset( $_POST['patient_id'] ) && true == valStr( $_POST['patient_id'] ) ) {
		$objPatient = $objPatient->fetchPatientsById( $_POST['patient_id'] );
	}
	
	if( true == isset( $_POST['case_paper_id'] ) && true == valStr( $_POST['case_paper_id'] ) ) {
		$objCasePaper = $objCasePaper->fetchCasePapersById( $_POST['case_paper_id'] );
		
		if( true == valObj( $objCasePaper, 'BaseCasePapers' ) ) {
			$intDiagnosisId = $objCasePaper->getDiagnosisId();
		}
	}
	
	if( false == valObj( $objPatient, 'BasePatients' ) || false == valObj( $objCasePaper, 'BaseCasePapers' ) ) {
		echo '0';
		exit;
	}
	
	if( true == isset( $_POST['ward_id'] ) && true == valStr( $_POST['ward_id'] ) ) {
		$objWard = $objWard->fetchWardsById( ( int ) $_POST['ward_id'] );
		
		if( true == valObj( $objWard, 'BaseWards' ) ) {
			if( true == valObj( $objPatient, 'BasePatients' ) ) {
				$intChargeAmount = PatientType::getRateByPatientType( $objPatient, $objWard->getRate() );
			}
		}
	}
	
	if( 'NULL' == $intDiagnosisId ) {
		$intDiagnosisId = '\'NULL\'';
	}
	
	$objMedicalCaseSheet 	= new BaseMedicalCaseSheets();
	
	$objMedicalCaseSheet->setWardId( $_POST['ward_id'] );
	$objMedicalCaseSheet->setPatientId( $_POST['patient_id'] );
	$objMedicalCaseSheet->setCasePaperId( $_POST['case_paper_id'] );
	$objMedicalCaseSheet->setAdmissionDate( date( 'Y-m-d' ) );
	$objMedicalCaseSheet->setDiagnosisId( $intDiagnosisId );
	$objMedicalCaseSheet->setDoctorId( ( true == isset( $_SESSION['SESS_MEMBER_ID'] ) ) ? $_SESSION['SESS_MEMBER_ID'] : 1 );
	$objMedicalCaseSheet->setDepartmentId( DepartmentType::DEPARTMENT_IPD );
	$objMedicalCaseSheet->setDischargeDate( 'NULL' );
	$objMedicalCaseSheet->setChargeTypeId( 1 );
	$objMedicalCaseSheet->setCreatedBy( ( true == isset( $_SESSION['SESS_MEMBER_ID'] ) ) ? $_SESSION['SESS_MEMBER_ID'] : 1 );
	$objMedicalCaseSheet->setCreatedOn( 'now()' );
	$objMedicalCaseSheet->setUpdatedBy( ( true == isset( $_SESSION['SESS_MEMBER_ID'] ) ) ? $_SESSION['SESS_MEMBER_ID'] : 1 );
	$objMedicalCaseSheet->setUpdatedOn( 'now()' );
	
	if( 1 == $objMedicalCaseSheet->insert() ) {
		echo '1';
	} else {
		echo '0';
	}
	
	exit;
} else if( true == isset( $_POST['operation'] ) && 'update' == strtolower( $_POST['operation'] ) ) {
	
	$objWards 		= new Wards();
	$objMedicalCaseSheet 	= $objWards->fetchWardsById( $_POST['ward_id'] );
	
	$objMedicalCaseSheet->setRoomNumber( $_POST['room_number'] );
	$objMedicalCaseSheet->setWardTypeId( $_POST['ward_type'] );
	$objMedicalCaseSheet->setRate( $_POST['rate'] );
	$objMedicalCaseSheet->setIsAvaliable( ( true == valStr( $_POST['is_avaliable'] ) ) ? $_POST['is_avaliable'] : 1 );
	$objMedicalCaseSheet->setMoveOutDate( ( true == valStr( $_POST['move_out_date'] ) ) ? $_POST['move_out_date'] : 'NULL' );
	$objMedicalCaseSheet->setReservationId( ( true == valStr( $_POST['reservation_id'] ) ) ? $_POST['reservation_id'] : 'NULL' );
	$objMedicalCaseSheet->setReportingDoctorId( ( true == valStr( $_POST['reporting_doctor_id'] ) ) ? $_POST['reporting_doctor_id'] : 'NULL' );
	$objMedicalCaseSheet->setPatientId( ( true == valStr( $_POST['patient_id'] ) ) ? $_POST['patient_id'] : 'NULL' );
	$objMedicalCaseSheet->setCasePaperId( ( true == valStr( $_POST['case_paper_id'] ) ) ? $_POST['case_paper_id'] : 'NULL' );
	$objMedicalCaseSheet->setDepartmentId( $_POST['department_id'] );
	$objMedicalCaseSheet->setFacilities( ( true == valStr( $_POST['facilities'] ) ) ? $_POST['facilities'] : 'NULL' );
	$objMedicalCaseSheet->setCreatedBy( ( true == isset( $_SESSION['SESS_MEMBER_ID'] ) ) ? $_SESSION['SESS_MEMBER_ID'] : 1 );
	$objMedicalCaseSheet->setUpdatedBy( ( true == isset( $_SESSION['SESS_MEMBER_ID'] ) ) ? $_SESSION['SESS_MEMBER_ID'] : 1 );
	$objMedicalCaseSheet->setUpdatedOn( 'now()' );
	
	if( 1 == $objMedicalCaseSheet->update() ) {
		$str = '';
		$str .= '<form name=myform method=post action=modules.php?file=view_wards&type=view&mid=' . $_REQUEST['mid'] . '&smid=' . $_REQUEST['smid'] . '>';
		$str .= '<script language=JavaScript>document.myform.submit();</script></form>';
		echo $str;
		exit;
	}
}

?>